﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;

namespace Wenyu.Framework.WebCore.SwaggerExtend
{
    public static class SwaggerExtension
    {
        /// <summary>
        /// 配置swagger的配置
        /// </summary>
        /// <param name="service"></param>
        public static void AddSwaggerExt(this IServiceCollection service,string docName="",string docDesc= "")
        {
            service.AddEndpointsApiExplorer();
            service.AddSwaggerGen(options =>
            {
                //支持多版本
                foreach (string version in typeof(ApiVersions).GetEnumNames())
                {
                    options.SwaggerDoc(version, new OpenApiInfo()
                    {
                        Title =!string.IsNullOrWhiteSpace(docName)?docName: $"文宇敏捷后台管理项目Api文档",
                        Version = version,
                        Description = !string.IsNullOrWhiteSpace(docDesc) ? docDesc : $"通用版本的CoreApi版本--{version}"
                    });
                }

                //xml文档绝对路径
                var file = Path.Combine(AppContext.BaseDirectory, $"{AppDomain.CurrentDomain.FriendlyName}.xml");
                //true：显示控制器层注释
                options.IncludeXmlComments(file, true);
                //对action的名称进行排序，如果有多个，就可以看见效果
                options.OrderActionsBy(o => o.RelativePath);

                #region 支持jwt token授权
                //添加安全定义
                options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = "JWT授权(数据将在请求头中进行传输) 在下方输入Bearer {token} 即可，注意两者之间有空格",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey, //jwt默认的参数名称
                    BearerFormat = "JWT",
                    Scheme = "Bearer"
                });

                #endregion
                //添加安全要求
                options.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference=new OpenApiReference()
                            {
                                Type=ReferenceType.SecurityScheme,
                                Id="Bearer"
                            }
                        },
                        new string[]{}
                    }
                });
            });
        }

        /// <summary>
        /// 使用
        /// </summary>
        /// <param name="app"></param>
        public static void UseSwaggerExt(this WebApplication app,string docName = "")
        {
            app.UseSwagger();
            app.UseSwaggerUI(options =>
            {
                foreach (string version in typeof(ApiVersions).GetEnumNames())
                {
                    options.SwaggerEndpoint($"/swagger/{version}/swagger.json", !string.IsNullOrWhiteSpace(docName) ? docName : $"文宇敏捷后台管理项目Api文档【{version}】版本");
                }
            });
        }
    }
}